package me.dz.dreamcleaner.manager;

import me.dz.dreamcleaner.DreamCleaner;
import me.mcdcs.dreambind.Api.DItem;
import me.mcdcs.dreambind.DreamBind;
import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.ArrayList;

import static me.dz.dreamcleaner.DreamCleaner.*;
import static me.dz.dreamcleaner.utils.ItemUtils.*;

public enum ItemType {
    ITEM{
        @Override
        public boolean canClear(Entity entity) {
            ItemStack itemStack = null;
            if (entity instanceof Item){
                itemStack = ((Item) entity).getItemStack();
            }
            if (itemStack != null){
                if (quickShop && ShopManager.isQuickShop((Item) entity)){
                    return false;
                }
                if (dreamBind){
                    DItem dItem = new DItem(itemStack);
                    if (dItem.isBind()){
                        if (dItem.getOwner() != null){
                            ItemStack finalItemStack = itemStack;
                            new BukkitRunnable(){
                                @Override
                                public void run() {
                                    entity.remove();
                                    DreamBind.addItem(finalItemStack,dItem.getOwner());
                                }
                            }.runTask(pl);
                        }
                        return false;
                    }
                }
                String cz = itemStack.getType().toString();
                if (itemType.size() > 0 && itemType.contains(cz)){
                    return false;
                }
                if (itemVagueType.size() > 0) {
                    for (String msg : itemVagueType){
                        if (cz.contains(msg)){
                            return false;
                        }
                    }
                }
                String name = getName(itemStack);
                if (!name.equals("")){
                    if (itemName .size() > 0 && itemName.contains(name)){
                        return false;
                    }
                    if (itemVagueName.size() > 0){
                        for (String msg : itemVagueName){
                            if (name.contains(msg)){
                                return false;
                            }
                        }
                    }
                }
                ArrayList<String> lore = getLore(itemStack);
                if (lore.size() > 0){
                    if (itemLore.size() > 0){
                        for (String msg : lore){
                            if (DreamCleaner.itemLore.contains(msg)){
                                return false;
                            }
                        }
                    }
                    if (itemVagueLore.size() > 0){
                        String ls = lore.toString();
                        for (String msg : itemVagueLore){
                            if (ls.contains(msg)){
                                return false;
                            }
                        }
                    }
                }
                return true;
            }
            return false;
        }
        @Override
        public boolean canRun() {
            return true;
        }
    },
    DENSEITEM{
        @Override
        public boolean canClear(Entity entity) {
            ItemStack itemStack = null;
            if (entity instanceof Item){
                itemStack = ((Item) entity).getItemStack();
            }
            if (itemStack != null){
                if (quickShop && ShopManager.isQuickShop((Item) entity)){
                    return false;
                }
                if (dreamBind){
                    DItem dItem = new DItem(itemStack);
                    if (dItem.isBind()){
                        if (dItem.getOwner() != null){
                            ItemStack finalItemStack = itemStack;
                            new BukkitRunnable(){
                                @Override
                                public void run() {
                                    entity.remove();
                                    DreamBind.addItem(finalItemStack,dItem.getOwner());
                                }
                            }.runTask(pl);
                        }
                        return false;
                    }
                }
                String cz = itemStack.getType().toString();
                if (denseType.size() > 0 && denseType.contains(cz)){
                    return false;
                }
                if (denseVagueType.size() > 0) {
                    for (String msg : denseVagueType){
                        if (cz.contains(msg)){
                            return false;
                        }
                    }
                }
                String name = getName(itemStack);
                if (!name.equals("")){
                    if (denseName .size() > 0 && denseName.contains(name)){
                        return false;
                    }
                    if (denseVagueName.size() > 0){
                        for (String msg : denseVagueName){
                            if (name.contains(msg)){
                                return false;
                            }
                        }
                    }
                }
                ArrayList<String> lore = getLore(itemStack);
                if (lore.size() > 0){
                    if (denseLore.size() > 0){
                        for (String msg : lore){
                            if (DreamCleaner.denseLore.contains(msg)){
                                return false;
                            }
                        }
                    }
                    if (denseVagueLore.size() > 0){
                        String ls = lore.toString();
                        for (String msg : denseVagueLore){
                            if (ls.contains(msg)){
                                return false;
                            }
                        }
                    }
                }
                return true;
            }
            return false;
        }

        @Override
        public boolean canRun() {
            return true;
        }
    },
    FRAME{
        @Override
        public boolean canClear(Entity entity) {
            return entity instanceof ItemFrame;
        }

        @Override
        public boolean canRun() {
            try {
                Object object = ItemFrame.class;
                return true;
            }catch (NoClassDefFoundError ignored){
                return false;
            }
        }
    },
    BOAT{
        @Override
        public boolean canClear(Entity entity) {
            if (entity instanceof Boat){
                return entity.getPassengers().size() == 0;
            }
            return false;
        }

        @Override
        public boolean canRun() {
            try {
                Object object = Boat.class;
                return true;
            }catch (NoClassDefFoundError ignored){
                return false;
            }
        }
    },
    Exp{
        @Override
        public boolean canClear(Entity entity) {
            return entity instanceof ExperienceOrb;
        }

        @Override
        public boolean canRun() {
            try {
                Object object = ExperienceOrb.class;
                return true;
            }catch (NoClassDefFoundError ignored){
                return false;
            }
        }
    },
    FALLINGBLOCK{
        @Override
        public boolean canClear(Entity entity) {
            return entity instanceof FallingBlock;
        }

        @Override
        public boolean canRun() {
            try {
                Object object = FallingBlock.class;
                return true;
            }catch (NoClassDefFoundError ignored){
                return false;
            }
        }
    },
    PAINTING{
        @Override
        public boolean canClear(Entity entity) {
            return entity instanceof Painting;
        }

        @Override
        public boolean canRun() {
            try {
                Object object = Painting.class;
                return true;
            }catch (NoClassDefFoundError ignored){
                return false;
            }
        }
    },
    MINECART{
        @Override
        public boolean canClear(Entity entity) {
            return entity instanceof Minecart;
        }

        @Override
        public boolean canRun() {
            try {
                Object object = Minecart.class;
                return true;
            }catch (NoClassDefFoundError ignored){
                return false;
            }
        }
    },
    ARROW{
        @Override
        public boolean canClear(Entity entity) {
            return entity instanceof Arrow;
        }

        @Override
        public boolean canRun() {
            try {
                Object object = Arrow.class;
                return true;
            }catch (NoClassDefFoundError ignored){
                return false;
            }
        }
    },
    SNOWBALL{
        @Override
        public boolean canClear(Entity entity) {
            return entity instanceof Snowball;
        }

        @Override
        public boolean canRun() {
            try {
                Object object = Snowball.class;
                return true;
            }catch (NoClassDefFoundError ignored){
                return false;
            }
        }
    },
    FIREBALL{
        @Override
        public boolean canClear(Entity entity) {
            return entity instanceof Fireball;
        }

        @Override
        public boolean canRun() {
            try {
                Object object = Fireball.class;
                return true;
            }catch (NoClassDefFoundError ignored){
                return false;
            }
        }
    },
    SHULKERBULLET{
        @Override
        public boolean canClear(Entity entity) {
            return entity instanceof ShulkerBullet;
        }

        @Override
        public boolean canRun() {
            try {
                Object object = ShulkerBullet.class;
                return true;
            }catch (NoClassDefFoundError ignored){
                return false;
            }
        }
    },
    ;

    public abstract boolean canClear(Entity entity);

    public abstract boolean canRun();
}
